module builder.Model;
import frontend.Types;
import frontend.Model;
import loader.scene.format.Hme;
import loader.scene.Node;
import loader.scene.Animation;
import utils.Memory;
import maths.Vec;
import maths.Quat;
import maths.CoordSys;

Model getModel(char[] filename, char[] modelname){
	HmeLoader hml;
	Model model=new Model(modelname);
	loader.scene.Animation.SkeletalAnimation skelAnim;
	hml= new HmeLoader;
	hml.load(filename);
	assert(hml !is null);
	foreach(node; hml.scene.nodes){
		if(node.name!=modelname) continue;

		model.indices=node.meshes[0].indices().clone();
		model.normals= node.meshes[0].normals().clone();
		model.positions=node.meshes[0].positions().clone();
		
		model.offsets.alloc(model.positions.length);
		model.bones.alloc(model.positions.length);
		model.weights.alloc(model.positions.length);
		
		skelAnim = cast(loader.scene.Animation.SkeletalAnimation)node.animation;
		
		foreach(index,vert; skelAnim.vertices){
			model.offsets[index]=vert.offsets().clone();
			model.bones[index]=vert.bones().clone();
			model.weights[index]=vert.weights().clone();
		}
		
		break;
	}
	return model;
}

Model getModel(char[] filename, char[] modelname, inout HmeLoader loaderref){
	HmeLoader hml;
	Model model=new Model(modelname);
	loader.scene.Animation.SkeletalAnimation skelAnim;
	hml= new HmeLoader;
	hml.load(filename);
	assert(hml !is null);
	foreach(node; hml.scene.nodes){
		if(node.name!=modelname) continue;

		model.indices=node.meshes[0].indices().clone();
		model.normals= node.meshes[0].normals().clone();
		model.positions=node.meshes[0].positions().clone();
		
		model.offsets.alloc(model.positions.length);
		model.bones.alloc(model.positions.length);
		model.weights.alloc(model.positions.length);
		
		skelAnim = cast(loader.scene.Animation.SkeletalAnimation)node.animation;
		
		foreach(index,vert; skelAnim.vertices){
			model.offsets[index]=vert.offsets().clone();
			model.bones[index]=vert.bones().clone();
			model.weights[index]=vert.weights().clone();
		}
		
		break;
	}
	loaderref=hml;
	return model;
}